#!/bin/bash

PATH="$PATH:/usr/local/bin"

# ABSOLUTE path to the PHP binary
PHPFCGI=$(which php-cgi) || PHPFCGI=$(which php5-cgi) || PHPFCGI=$(which php)

# tcp-port to bind on
FCGIPORT="5859"

# IP to bind on
FCGIADDR="127.0.0.1"

# number of PHP children to spawn
PHP_FCGI_CHILDREN=8

# number of request before php-process will be restarted
PHP_FCGI_MAX_REQUESTS=1000

# the IP addresses of the servers who can talk to fastcgi
FCGI_WEB_SERVER_ADDRS='127.0.0.1'

# allowed environment variables sperated by spaces
ALLOWED_ENV="PATH USER"

# if this script is run as root switch to the following user
#USERID=nobody


if test x$PHP_FCGI_CHILDREN = x; then
  PHP_FCGI_CHILDREN=5
fi

ALLOWED_ENV="$ALLOWED_ENV PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS FCGI_WEB_SERVER_ADDRS"

if test x$UID = x0; then
  EX="/bin/su -m -c \"$PHPFCGI -q -b $FCGIADDR:$FCGIPORT\" $USERID &"
else
  EX="$PHPFCGI -b $FCGIADDR:$FCGIPORT &"
fi

#echo $EX

# copy the allowed environment variables
E=

for i in $ALLOWED_ENV; do
  E="$E $i=${!i}"
done

# clean environment and set up a new one with php running as fastcgi
nohup env - $E sh -c "$EX" &> /dev/null </dev/null
